package com.grandbroad.eim.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.flyco.tablayout.SlidingTabLayout;
import com.grandbroad.eim.R;
import com.grandbroad.eim.base.BaseActivity;
import com.grandbroad.eim.entry.LiveRoomBean;
import com.grandbroad.eim.service.ChatRoomService;
import com.grandbroad.eim.ui.fragment.live.ChatRoomFragment;
import com.grandbroad.eim.ui.fragment.live.LiveDesFragment;
import com.grandbroad.eim.utils.ToastUtils;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jpush.im.android.api.ChatRoomManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.RequestCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import dev.utils.app.AppCommonUtils;
import dev.utils.app.AppUtils;
import dev.utils.app.wifi.WifiUtils;

/**
 * 直播页面
 */
public class LiveActivity extends BaseActivity {
    @BindView(R.id.player)
    JzvdStd player;
    @BindView(R.id.sliding_tab)
    SlidingTabLayout slidingTab;
    @BindView(R.id.viewpage_video)
    ViewPager viewpageVideo;
    private List<Fragment> fragments = new ArrayList<>();
    private ChatRoomFragment roomFragment;
    private LiveDesFragment desFragment;
    private List<String> titles = new ArrayList<>();
    private MyLiveAdapter liveAdapter;
    //需要一个直播间room
    private LiveRoomBean liveRoomBean;
    private int chatroomid;
    private Intent service_chat;
    @Override
    protected void initview() {


    }

    @Override
    protected void initdata() {
        //获取数据源
        liveRoomBean = (LiveRoomBean) getIntent().getExtras().getSerializable("bean");
        //这里要进入聊天室
        //获取目标房间对accesstoken
        String phone = liveRoomBean.getUser().getPhone();
        chatroomid = liveRoomBean.getChatRoomid();
        ChatRoomManager.enterChatRoom(chatroomid, new RequestCallback<Conversation>() {
            @Override
            public void gotResult(int i, String s, Conversation conversation) {
                switch (i)
                {
                    case 0:
                        //为进入成功的情况
                        Log.e("聊天室","进入成功");
                        break;
                    case -1:
                        //未知状态
                        ToastUtils.show("链接聊天室失败，请重试");
                        break;
                }
            }
        });
        initplayer();
        initFragment();
    }

    private void initplayer() {
        JZDataSource dataSource = new JZDataSource(liveRoomBean.getRtmp());
        dataSource.title = liveRoomBean.getName();
        //封面加载
        x.image().bind(player.thumbImageView,liveRoomBean.getThumbnail());
        player.setUp(dataSource,Jzvd.SCREEN_WINDOW_NORMAL);
    }

    private void initFragment() {
        roomFragment = new ChatRoomFragment();
        desFragment = new LiveDesFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("bean",liveRoomBean);
        desFragment.setArguments(bundle);
        roomFragment.setArguments(bundle);
        fragments.add(desFragment);
        fragments.add(roomFragment);
        titles.add("简介");
        titles.add("聊天室");
        liveAdapter = new MyLiveAdapter(getSupportFragmentManager());
        viewpageVideo.setAdapter(liveAdapter);
        slidingTab.setViewPager(viewpageVideo);

    }

    @Override
    protected int setlayout() {
        return R.layout.activity_live;
    }

    /**
     * 页面适配器
     */
    class MyLiveAdapter extends FragmentStatePagerAdapter
    {

        public MyLiveAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return 2;
        }


        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }


    @Override
    public void onBackPressed() {
        //先判断服务是否启动

        //启动服务
        service_chat = new Intent(this,ChatRoomService.class);
        startService(service_chat);
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        /*
        ChatRoomManager.leaveChatRoom(chatroomid, new RequestCallback<Conversation>() {
            @Override
            public void gotResult(int i, String s, Conversation o) {
                if (i==0)
                {
                    Log.e("退出聊天室成功","成功");
                }
            }
        });
        */
        super.onDestroy();
    }
}
